#include "serverconnection.h"

// Qt Includes
#include <QTcpSocket>

// Local Includes
#include "servernetengine.h"

/////////////////////////////////////////////////////////////////////
// Construction & Destruction
/////////////////////////////////////////////////////////////////////
CServerConnection::CServerConnection(QObject *pParent)
	: QTcpServer(pParent)
{

}

CServerConnection::CServerConnection()
{
	qDeleteAll(m_lstServerClients);
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
// Member Functions
/////////////////////////////////////////////////////////////////////
bool CServerConnection::listen(quint16 nPort)
{
	// It's considered invalid to try and initialise a connection that has already been
	// initialised.
	if(isListening())
		return true;

	if(nPort == 0)
		return false;

	// Start listening.
	if(!QTcpServer::listen(QHostAddress::Any, nPort))
		return false;

	return true;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
// Overrides
/////////////////////////////////////////////////////////////////////
void CServerConnection::incomingConnection(int socketDescriptor)
{
	QThread *pClientThread = new CServerNetEngine(socketDescriptor);

	// Signals & Slots.
	connect(pClientThread, SIGNAL(finished()), pClientThread, SLOT(deleteLater()));

	// Start up the thread, which will handle the network processing.
	pClientThread->start();
}
/////////////////////////////////////////////////////////////////////
